<template>
  <div class="addEquipment">
    <div>
      <editForm :formConfig="equipConfig" ref="basicForm" :formData="formData">
        <template #standardName="{ editFormInfo }">
          <el-input
            size="mini"
            :title="editFormInfo.standardName"
            v-model="editFormInfo.standardName"
            :placeholder="'请选择'"
            :disabled="disabled"
            readonly
          >
            <el-button
              slot="append"
              :disabled="disabled"
              @click="openDialog(editFormInfo)"
              >选择</el-button
            >
          </el-input>
        </template>
      </editForm>
    </div>
    <div>
      <checkItem
        :checkItemList="checkItemList"
        :paramsItemList="paramsItemList"
        :unEdit="unEdit"
        ref="checkItem"
      />
    </div>
    <dialogPublic
      :title="title"
      :show.sync="show"
      width="90%"
      :modal="false"
      @doSubmit="doSubmit"
      @doClose="() => (show = false)"
    >
      <div style="height: 600px">
        <relatePage
          v-if="show"
          :searchData="searchData"
          ref="relatePage"
        ></relatePage>
      </div>
    </dialogPublic>
  </div>
</template>

<script>
import { queryPatrolStandardDetail } from "@/api/standardManagement/patrolStandard.js";
import checkItem from "./checkItem.vue";
import relatePage from "@/views/standardManagement/patrolStandard/relatePage.vue";
export default {
  props: {
    rowInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
    unEdit: {
      type: Boolean,
      default: false,
    },
  },
  components: {
    checkItem,
    relatePage,
  },
  data() {
    return {
      title: "选择巡视标准",
      show: false,
      disabled: false,
      searchData: {},
      equipConfig: {
        labelWidth: "120px",
        itemArr: [
          {
            label: "巡视设备",
            prop: "equipmentName",
            idsStr: "equipmentId",
            filterConfig: [],
            standardType: "",
            type: "device",
            span: 8,
            required: true,
            disabled: false,
            methods: (data, formData) => {
              this.$refs.basicForm.setFormItem("typeId", data[0].typeId);
              this.$refs.basicForm.setFormItem("typeName", data[0].typeName);
              this.formData.typeId = data[0].typeId;
              this.formData.typeName = data[0].typeName;
            },
          },
          {
            label: "巡视标准",
            prop: "standardName",
            idsStr: "standardId",
            type: "text",
            required: false,
            disabled: false,
            span: 8,
          },
        ],
      },
      formData: {},
      checkItemList: [],
      paramsItemList: [],
      //基本参数
    };
  },
  watch: {
    rowInfo: {
      handler(val) {
        if (JSON.stringify(val) != "{}") {
          this.formData = val;
          this.checkItemList = val.items;
          this.paramsItemList = val.records;
        } else {
          this.formData = {};
          this.checkItemList = [];
          this.paramsItemList = [];
        }
      },
      deep: true,
    },
  },
  created() {
    if (JSON.stringify(this.rowInfo) != "{}") {
      this.formData = this.rowInfo;
      this.checkItemList = this.rowInfo.items;
      this.paramsItemList = this.rowInfo.records;
    }
    if (this.unEdit) {
      this.equipConfig.itemArr.map((i) => (i.disabled = true));
      this.disabled = true;
    }
  },
  methods: {
    //打开巡视标准弹框
    openDialog() {
      let appId = this.$store.getters.currentAppInfo.appId;
      this.searchData = {
        typeWrapId: this.formData.typeId,
        typeWrapName: this.formData.typeName,
        publicStatus: 1,
        appids:[appId]
      };
      this.show = true;
    },
    //获取标准详情
    getDetails(id) {
      queryPatrolStandardDetail({ id: id }).then((res) => {
        if (res.code == 200) {
          let checkList = JSON.parse(JSON.stringify(res.data.checks));
          this.checkItemList = checkList.map((item) => {
            const obj = {
              runState: item.status,
              itemName: item.name,
              itemValue: item.content,
            };
            return obj;
          });
          let paramsList = JSON.parse(JSON.stringify(res.data.records));
          this.paramsItemList = paramsList.map((item) => {
            const obj = {
              runState: item.status,
              itemName: item.name,
              itemUnit: item.unit,
              defaultValue: item.defaultValue,
              dataStandard: item.dataLimit,
            };
            return obj;
          });
        }
      });
    },
    //提交选择标准
    doSubmit() {
      let data = this.$refs.relatePage.getSelectData();
      if (data) {
        this.$refs.basicForm.setFormItem("standardId", data.id);
        this.$refs.basicForm.setFormItem("standardName", data.name);
        this.getDetails(data.id);
        this.show = false;
      } else {
        this.$message.warning("请选择一条标准");
      }
    },
    //获取所有信息
    async getAllInfo() {
      let formData = await this.$refs.basicForm.getFormData();
      let tableData = this.$refs.checkItem.getCheckTableData();
      formData.items = tableData.checkItem;
      formData.records = tableData.paramsItem;
      return formData;
    },
  },
};
</script>

<style lang="less" scoped>
.addEquipment {
  width: 100%;
  height: 100%;
  padding: 0 30px;
}
</style>
